package com.atguigu.common.valid;

import com.google.common.collect.Sets;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;

public class GulimallValidConstraintValidator implements ConstraintValidator<GulimallValid, Integer> {

    private Set<Integer> set = Sets.newHashSet();
    @Override
    public void initialize(GulimallValid constraintAnnotation) {
        Arrays.stream(constraintAnnotation.vals()).filter(Objects::nonNull).forEach(n->set.add(n));
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
